package cn.dujc.superkid.activities;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.Fragement_testAdapter;
import cn.dujc.superkid.adapter.MyCourseViewPagerAdapter;
import cn.dujc.superkid.fragment.MyCourserFragment.CupGameFragment;
import cn.dujc.superkid.fragment.MyCourserFragment.KnowledgeFragment;

public class MyCourseActivity extends BaseActivity {
    private KnowledgeFragment knowledgeFragment;
    private CupGameFragment cupGameFragment;
    private TabLayout tabLayout = null;
    private ViewPager vp_pager;
    private List<Fragment> fragments;
    private MyCourseViewPagerAdapter adapter;
    @Override
    public int getViewId() {
        return R.layout.activity_my_course;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initViews();
        initLogic();
        setTitle("我的课程");
    }
    private void initLogic() {
        List<String> titles = new ArrayList<>();
        titles.add("知识点");
        titles.add("杯赛");

        for (int i = 0; i < titles.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText(titles.get(i)));

        }
        knowledgeFragment  = new KnowledgeFragment();
        cupGameFragment = new CupGameFragment();
        fragments = new ArrayList<>();
        fragments.add(knowledgeFragment);
        fragments.add(cupGameFragment);

        adapter = new MyCourseViewPagerAdapter(getSupportFragmentManager(),fragments,titles);
        //viewpager绑定适配器
        vp_pager.setAdapter(adapter);
        //tabLayout绑定viewpager
        tabLayout.setupWithViewPager(vp_pager);


        //适配器
        Fragement_testAdapter fragementAdapter = new Fragement_testAdapter(getSupportFragmentManager(), fragments, titles);
        //给ViewPager设置adapter
        vp_pager.setAdapter(fragementAdapter);
        //将Tablayout和ViewPager关联起来

        tabLayout.setupWithViewPager(vp_pager);
        //给TaBlayout设置适配器
        tabLayout.setTabsFromPagerAdapter(fragementAdapter);
    }

    private void initViews() {
        tabLayout = findViewById(R.id.course_tablayout);
        vp_pager = findViewById(R.id.course_viewpager);
        tabLayout.setupWithViewPager(vp_pager);
    }

}
